/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.helpdesk.beans;

import com.helpdesk.controllers.util.LazySorter;
import com.helpdesk.entities.Categories;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

/**
 *
 * @author JK
 */
public class CategoriesLazyModel extends LazyDataModel<Categories> {

    List<Categories> items;

    public CategoriesLazyModel(List<Categories> items) {
        this.items = items;
    }

    public Categories getRowData(int id) {
        for (Categories c : items) {
            if (c.getId() == id) {
                return c;
            }
        }
        return null;
    }

    @Override
    public Categories getRowData(String rowKey) {
        try {
            int id = Integer.parseInt(rowKey);
            if (id > 0) {
                return getRowData(id);
            }
            return null;
        } catch (Exception e) {
            return null;
        }

    }

    @Override
    public Object getRowKey(Categories cat) {
        return cat.getId();
    }

    @Override
    public List<Categories> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
        List<Categories> cats = new ArrayList<Categories>();
        //filter
        for (Categories cat : items) {
            boolean match = true;
            for (Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {
                try {
                    //get filter property
                    String filterProperty = it.next();
                    //get filter value
                    String filterValue = filters.get(filterProperty).toLowerCase();
                    //get field value (reflection)
                    Field field = cat.getClass().getDeclaredField(filterProperty);
                    field.setAccessible(true);
                    String fieldValue = String.valueOf(field.get(cat)).toLowerCase();
                    //logic 
                    if (filterValue == null || fieldValue.startsWith(filterValue)) {
                        match = true;
                    } else {
                        match = false;
                        break;
                    }
                } catch (Exception e) {
                    match = false;
                }
                
            }
            if (match) {
                    cats.add(cat);
                }
        }
        if(sortField!=null){
            Collections.sort(cats,new LazySorter<Categories>(sortField, sortOrder));
        }
        //rowCount  
        int dataSize = cats.size();  
        this.setRowCount(dataSize);  
        //paginate  
        if(dataSize > pageSize) {  
            try {  
                return cats.subList(first, first + pageSize);  
            }  
            catch(IndexOutOfBoundsException e) {  
                return cats.subList(first, first + (dataSize % pageSize));  
            }  
        }  
        else {  
            return cats;  
        }
    }
}
